<?php
/**
 * 类名称
 * User: 13651
 * Time: 2025/9/19 17:36
 */

namespace App\Services;

use App\Api\Controllers\LeshuaUnion\LeshuaBaseController as LeshuaUnion;
use App\Repository\StorePayRepo;
use Exception;
use App\Extensions\BaseService;
use App\Param\LeshuaUnion\GetChannelDetailsParams;
use App\Repository\LeshuaUnionStoresRepo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use App\Traits\ResponseTrait;

class LeshuaUnionStoresService   extends BaseService
{
    use ResponseTrait;

    /**
     * 数据入库
     * @param array $data
     * @param int $id
     * @return \Illuminate\Support\HigherOrderCollectionProxy|int|mixed
     */
    public function saveDataService(array $data,int $id=0)
    {
        return LeshuaUnionStoresRepo::getInstance()->saveDataRepo($data,$id);
    }

    /**
     * 根据商户编号获取信息
     * @param string $id
     * @return Builder|Model|object|null
     */
    public function getByStoreIdService(string $id)
    {
        return LeshuaUnionStoresRepo::getInstance()->getByStoreId($id);
    }

    /**
     * 获取商户状态
     * @param GetChannelDetailsParams $params
     * @return mixed
     * @throws BizException
     */
    public function getChannelStatus(GetChannelDetailsParams $params)
    {
        $info = LeshuaUnionStoresRepo::getInstance()->getByStoreId($params->getStoreId());
        if (empty($info))
            return $this->errorResponse('商户信息不存在');

        /**
         * 根据商户号获取代理商编号
         */
        $configInfo = LeshuaUnionConfigService::getInstance()->getByConfigIdService($configId = 1234);
        if (empty($configInfo))
            return $this->errorResponse('代理商信息不存在');

        $config = [
            'orgId'=>$configInfo->orgId,
            'key'=>$info->key,
        ];

        $data =[
            'merchantId'=>$info->mch_id,
        ];
        $result = LeshuaUnion::getInstance()->getMerchantDetail($config,$data);
        Log::info('乐刷联合支付查询商户详情返回报文'.$result);
        if (empty($result))
            return $this->errorResponse('获取详情失败');

        $details = json_decode($result,true);
        if ($details['error_code'] != '0')
            return $this->errorResponse($details['error_msg']);

        $status = $details['data']['merchantBaseInfo']['status'];
        /**
         * 通过商户状态码更新
         */
        $where = [ 'store_id' => $params->getStoreId(),"company"=>"leshuaunion" ];
        if ($status == 1)
        {
            $data = [ 'status' => 1, 'status_desc' => '开通成功' ,'updated_at'=>date('Y-m-d H:i:s') ];
        }else{
            $data = [ 'status' => 3, 'status_desc' => '开通失败'.$details['data']['merchantBaseInfo']['noPassReason'],'updated_at'=>date('Y-m-d H:i:s') ];
        }
        $res = StorePayRepo::getInstance()->savaData($where,$data);
        if (!$res)
            return $this->errorResponse('系统异常');
        /**
         * 商户状态码
         * 0 待审核
         * 1 审核通过
         * 2 审核和不通过
         * 4 资料待完善
         * -1 未知
         */
        return ['status'=>$status,'message'=>$details['data']['merchantBaseInfo']['noPassReason'] ?$details['data']['merchantBaseInfo']['noPassReason']:$details['error_msg']];
    }
}